package com.hzw.saas.service.group.service;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.IService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hzw.saas.api.group.IGroupMemberRoleRefService;
import com.hzw.saas.common.config.util.AssertUtil;
import com.hzw.saas.service.group.mapper.GroupMemberRoleRefMapper;
import com.hzw.saas.service.group.model.GroupMemberRoleRef;
import org.springframework.stereotype.Service;
import jodd.util.StringUtil;
import lombok.AllArgsConstructor;

/**
 * <p>
 * 用户成员角色关联表 服务实现类
 * </p>
 *
 * @author sonam
 * @since 2020-12-23
 */
@AllArgsConstructor
@Service("groupMemberRoleRefServiceImpl")
public class GroupMemberRoleRefServiceImpl extends ServiceImpl<GroupMemberRoleRefMapper, GroupMemberRoleRef> implements IGroupMemberRoleRefService, IService<GroupMemberRoleRef> {

    @Override
    public void updateMemberRole(String memberId, String roleId) {
        AssertUtil.assertThrow("设置角色失败，请重试", StringUtil.isAllEmpty(memberId, roleId));
        GroupMemberRoleRef roleRef = new GroupMemberRoleRef();
        roleRef.setMemberId(memberId);
        roleRef.setRoleId(roleId);
        if(count(Wrappers.<GroupMemberRoleRef>lambdaQuery().eq(GroupMemberRoleRef::getMemberId, memberId)) > 0) {
            this.update(roleRef,
                    Wrappers.<GroupMemberRoleRef>lambdaQuery().eq(GroupMemberRoleRef::getMemberId, memberId));
        }
        AssertUtil.assertThrow("设置权限失败", true);
    }

    @Override
    public void addMemberRole(String memberId, String roleId) {

        AssertUtil.assertThrow("添加角色失败，请重试", StringUtil.isAllEmpty(memberId, roleId));

        GroupMemberRoleRef roleRef = new GroupMemberRoleRef();
        roleRef.setMemberId(memberId);
        roleRef.setRoleId(roleId);
        this.save(roleRef);
    }
}
